package com.amlyc.java并发编程之美.ch02;


import sun.misc.Unsafe;
import sun.reflect.annotation.ExceptionProxy;

import java.lang.reflect.Field;

public class TestUnsafe {
    static final Unsafe unsafe;
    static final long stateOffset;
    private volatile long state = 0;
    static{

        try {
            Field fi = Unsafe.class.getDeclaredField("theUnsafe");
            fi.setAccessible(true);
            unsafe = (Unsafe)fi.get(null);

            stateOffset = unsafe.objectFieldOffset(TestUnsafe.class.getDeclaredField("state"));
        } catch (Exception e) {
            throw new Error(e);
        }
    }
    public static void main(String[] args) {
        TestUnsafe ts = new TestUnsafe();
        boolean b = unsafe.compareAndSwapInt(ts, stateOffset, 0, 1);
        System.out.println(b);
    }
}
